<?php

namespace api\modules\app\models;

use Yii;

/**
 * This is the model class for table "unit".
 *
 * @property integer $id
 * @property string $unit_code
 * @property string $outer_code
 * @property string $district_code
 * @property string $street_code
 * @property string $community_code
 * @property string $estate_code
 * @property string $grid_code
 * @property string $name
 * @property string $name_code
 * @property string $event_type1
 * @property string $event_type1_code
 * @property string $event_type2
 * @property string $event_type2_code
 * @property string $event_type3
 * @property string $event_type3_code
 * @property string $images
 * @property double $lat
 * @property double $lng
 * @property double $lat_cd
 * @property double $lng_cd
 * @property double $lat_gcj
 * @property double $lng_gcj
 * @property string $address
 * @property string $dep_code
 * @property string $section
 * @property string $section_start
 * @property string $section_end
 * @property integer $state
 * @property integer $has_bind
 * @property integer $bind_iot
 * @property integer $status
 * @property integer $channel
 * @property integer $audit_state
 * @property integer $audit_time
 * @property integer $cuser
 * @property string $cuser_name
 * @property integer $ctime
 * @property integer $utime
 * @property integer $qrcode
 * @property string $iot_code
 * @property integer $def
 * @property integer $type
 * @property string $loc_type_code
 * @property string $loc_type_name
 */
class Unit extends BaseModel
{
    const TYPE_UNIT = 1; //部件
    const TYPE_LOCATION = 2; //点位



    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'unit';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('test_3');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['unit_code', 'utime'], 'required'],
            [['images', 'address', 'iot_code','other_dep'], 'string'],
            [['lat', 'lng', 'lat_cd', 'lng_cd', 'lat_gcj', 'lng_gcj'], 'number'],
            [['state', 'has_bind', 'bind_iot', 'status', 'channel', 'audit_state', 'audit_time', 'cuser', 'ctime', 'utime', 'def', 'type'], 'integer'],
            [['type_code','unit_code', 'outer_code', 'district_code', 'street_code', 'community_code', 'estate_code', 'grid_code', 'event_type1', 'event_type1_code', 'event_type2', 'event_type2_code', 'event_type3', 'event_type3_code', 'dep_code', 'cuser_name', 'qrcode'], 'string', 'max' => 5000],
            [['name', 'name_code'], 'string', 'max' => 100],
            [['section', 'section_start', 'section_end', 'loc_type_code', 'loc_type_name','loc_name'], 'string', 'max' => 255],
            [['unit_code'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'unit_code' => 'Unit Code',
            'outer_code' => 'Outer Code',
            'district_code' => 'District Code',
            'street_code' => 'Street Code',
            'community_code' => 'Community Code',
            'community_name' => 'Community name',
            'estate_code' => 'Estate Code',
            'estate_name' => 'Estate name',
            'grid_code' => 'Grid Code',
            'name' => 'Name',
            'name_code' => 'Name Code',
            'event_type1' => 'Event Type1',
            'event_type1_code' => 'Event Type1 Code',
            'event_type2' => 'Event Type2',
            'event_type2_code' => 'Event Type2 Code',
            'event_type3' => 'Event Type3',
            'event_type3_code' => 'Event Type3 Code',
            'images' => 'Images',
            'lat' => 'Lat',
            'lng' => 'Lng',
            'lat_cd' => 'Lat Cd',
            'lng_cd' => 'Lng Cd',
            'lat_gcj' => 'Lat Gcj',
            'lng_gcj' => 'Lng Gcj',
            'address' => 'Address',
            'dep_code' => 'Dep Code',
            'section' => 'Section',
            'section_start' => 'Section Start',
            'section_end' => 'Section End',
            'state' => 'State',
            'has_bind' => 'Has Bind',
            'bind_iot' => 'Bind Iot',
            'status' => 'Status',
            'channel' => 'Channel',
            'audit_state' => 'Audit State',
            'audit_time' => 'Audit Time',
            'cuser' => 'Cuser',
            'cuser_name' => 'Cuser Name',
            'ctime' => 'Ctime',
            'utime' => 'Utime',
            'qrcode' => 'Qrcode',
            'iot_code' => 'Iot Code',
            'def' => 'Def',
            'type_code' => 'Type Code'
        ];
    }

    public function fields()
    {
        return array_merge(parent::fields(), [
            'meta_level5_name' => function () {
                return $this->community_name;
            },
            'meta_level6_name' => function () {
                return $this->estate_name;
            },
            'meta_level1' => function () {
                return 'sh01';
            },
            'meta_level2' => function () {
                return 'sh0101';
            },
            'meta_level3' => function () {
                return $this->district_code;
            },
            'meta_level4' => function () {
                return $this->street_code;
            },
            'meta_level5' => function () {
                return $this->community_code;
            },
            'meta_level6' => function () {
                return $this->estate_code;
            },
        ]);
    }

    public function beforeValidate()
    {
        $this->utime = time();
        return parent::beforeValidate(); // TODO: Change the autogenerated stub
    }

    public static function getTagCodeList() {
        return [
            self::TAG_CODE_UNIT => '部件',
            self::TAG_CODE_LOCATION => '点位',
        ];
    }

    public static function getTagCodeByKey($key) {
        $result = '';
        $list = self::getTagCodeList();
        if (array_key_exists($key, $list)) {
            $result = $list[$key];
        }
        return $result;
    }

    public static function getLocTypeList() {
        return [
            1 => '小区',
            2 => '场馆',
            3 => '公园'
        ];
    }

    public static function getLocTypeByKey($key) {
        $result = '';
        $list = self::getLocTypeList();
        if (array_key_exists($key, $list)) {
            $result = $list[$key];
        }
        return $result;
    }
}
